package com.cap.bts.crmdp.dealer.meal.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.coupon.dao.ITicKetDefineDao;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketLine;
import com.cap.bts.crmdp.dealer.meal.constants.MealConstants;
import com.cap.bts.crmdp.dealer.meal.dao.IMealLineManageDao;
import com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao;
import com.cap.bts.crmdp.dealer.meal.dao.IPackageDealerSelectedDao;
import com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao;
import com.cap.bts.crmdp.dealer.meal.domain.MealModel;
import com.cap.bts.crmdp.dealer.meal.domain.MeallineModel;
import com.cap.bts.crmdp.dealer.meal.domain.PackageDealerSelected;
import com.cap.bts.crmdp.dealer.meal.domain.PackageEligibilityModel;
import com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto;
import com.cap.bts.crmdp.dealer.meal.dto.MealLineResultDto;
import com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto;
import com.cap.bts.crmdp.dealer.meal.service.IMealManageService;
import com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao;
import com.cap.bts.crmdp.dealer.packagegift.domain.MemberGiftInfo;
import com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto;
import com.cap.bts.framework.common.exception.AppException;

/**
 * 套餐信息业务处理
 * 
 * @author liZhiYong
 * @version $Id: MealManageServiceImpl.java, v 0.1 2015-9-14 下午4:22:39 1111 Exp
 *          $
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class MealManageServiceImpl implements IMealManageService {
	private static final Logger LOGGER = Logger.getLogger(MealManageServiceImpl.class);
    @Autowired
    IMealManageDao       iMealManageDao;

    @Autowired
    IMealLineManageDao   iMealLineManageDao;

    @Autowired
    ITicKetDefineDao     iTicKetDefineDao;

    @Autowired
    ITicketLineDefineDao iTicketLineDefineDao;
    
    @Autowired
    IPackageEligibilityDao iPackageEligibilityDao;
    @Autowired
    IPackageDealerSelectedDao packageDealerSelectedDao;

    @Resource(name = "memberGiftDao")
    IMemberGiftDao       iMemberGiftDao;

    /**
     * 套餐信息查询
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealLIst(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public List<MealModel> getMealLIst(MealDefineDto dto) throws AppException {
        return iMealManageDao.getMealList(dto);
    }

    /**
     * 新增套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#insertMealInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public MealDefineDto insertMealInfo(MealDefineDto dto) throws AppException {
        MealDefineDto mealDefineDto2 = new MealDefineDto();
        MealModel mealModel = new MealModel();
        mealModel.setCode(dto.getCode());
        mealModel.setName(dto.getName());
        mealModel.setDescription(dto.getDescription());
        mealModel.setStatus(dto.getStatus());
        mealModel.setPrice(dto.getPrice());
        mealModel.setValidPeriod(dto.getValidPeriod());
        mealModel.setUnitOfPeriod(dto.getUnitOfPeriod());
        mealModel.setIsPhysicalCard(dto.getIsPhysicalCard());
        mealModel.setIsPrepaymentCard(dto.getIsPrepaymentCard());
        mealModel.setNumberSequence(dto.getNumberSequence());
        mealModel.setCardDefId(dto.getCardDefId());
        mealModel.setMemberBenfit(dto.getMemberBenfit());

        MealModel mealModel2 = iMealManageDao.insertMealInfo(mealModel);
        mealDefineDto2.setFlag(false);
        if (mealModel2 != null) {
            //套餐详细信息新增
            String dataStr = dto.getDataStr();
            MealDefineDto mealDefineDto = null;
            if (!dataStr.isEmpty()) {
                String[] data = dataStr.split("###");
                for (int i = 0; i < data.length; i++) {
                    String[] str = data[i].split("@#");
                    String ticketDefId = str[0];
                    String shortName = str[1];
                    String numberOfUse = str[2];

                    if (!ticketDefId.isEmpty()) {
                        Ticket ticket = iTicKetDefineDao.getTicketById(ticketDefId);

                        mealDefineDto = new MealDefineDto();
                        mealDefineDto.setPackageDefId(mealModel.getId());
                        mealDefineDto.setTicketDefId(ticket.getId());
                        mealDefineDto.setNumberOfUse(Integer.valueOf(numberOfUse));
                        mealDefineDto.setShortName(shortName);

                        if (MealConstants.VERIFICATION_METHOD_CODE.equals(ticket
                            .getVerificationMethod())) {
                            mealDefineDto.setHasVerificationCode("Y");
                        } else if (MealConstants.VERIFICATION_METHOD_N.equals(ticket
                            .getVerificationMethod())) {
                            mealDefineDto.setHasVerificationCode("N");
                        }

                        MeallineModel meallineModel = insertMealLineInfo(mealDefineDto);
                        if (meallineModel != null) {
                            mealDefineDto2.setFlag(true);
                        }
                    }
                }
            }

            //获取入会礼信息
            String giftStr = dto.getGiftStr();
            if (!"".equals(giftStr) && giftStr != null) {
                String[] gift = giftStr.split("###");
                for (int i = 0; i < gift.length; i++) {
                    String[] str = gift[i].split("@#");
                    String giftName = str[0];
                    String giftType = str[1];
                    if (giftName.indexOf("_") > 0) {
                        giftName = giftName.substring(giftName.indexOf("_") + 1);
                    }
                    MermberGiftDto mermberGiftDto = new MermberGiftDto();
                    mermberGiftDto.setPackageId(mealModel2.getId());
                    mermberGiftDto.setGiftType(giftType);
                    mermberGiftDto.setGiftId(Long.parseLong(giftName));
                    if (iMemberGiftDao.saveMermberGiftInfo(mermberGiftDto)) {
                        mealDefineDto2.setFlag(true);
                    }
                }
            }
        }
        return mealDefineDto2;
    }

    /**
     * 新增套餐的详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#insertMealLineInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public MeallineModel insertMealLineInfo(MealDefineDto dto) throws AppException {
        MeallineModel meallineModel = new MeallineModel();
        meallineModel.setPackageDefId(dto.getPackageDefId());
        meallineModel.setShortName(dto.getShortName());
        meallineModel.setDescription(dto.getDescription());
        meallineModel.setNumberOfUse(dto.getNumberOfUse());
        meallineModel.setHasVerificationCode(dto.getHasVerificationCode());
        meallineModel.setTicketDefId(dto.getTicketDefId());

        MeallineModel meallineModel2 = iMealLineManageDao.insertMealLineInfo(meallineModel);
        if (meallineModel2 != null && "edit".equals(dto.getTemp())) {
            //根据套餐ID获取套餐的详细信息
            List<MeallineModel> meallineList = getMealLIneInfoByPackageId(dto.getPackageDefId());

            Double sumAmount = 0.00;
            for (Iterator iterator = meallineList.iterator(); iterator.hasNext();) {
                MeallineModel mealline = (MeallineModel) iterator.next();

                //根据ticketId查询优惠券和提货券的详细信息
                Long ticketId = mealline.getTicketDefId();
                List<TicketLine> ticketlineList = iTicketLineDefineDao.getTicketLineInfoList(Long
                    .toString(ticketId));

                if (!ticketlineList.isEmpty()) {
                    Double prepayAmount = 0.00;
                    for (Iterator iterator2 = ticketlineList.iterator(); iterator2.hasNext();) {
                        TicketLine ticketLine = (TicketLine) iterator2.next();
                        if (ticketId.equals(ticketLine.getTicketDefId())) {
                            prepayAmount = prepayAmount + ticketLine.getPrepayAmount();
                        }
                    }
                    double amount = prepayAmount * mealline.getNumberOfUse();
                    BigDecimal bigAmount = new BigDecimal(amount);

                    amount = bigAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sumAmount = sumAmount + amount;
                }
            }

            MealModel mealModel = iMealManageDao.findById(dto.getPackageDefId());
            mealModel.setPrice(sumAmount);
            iMealManageDao.update(mealModel);
        }

        return meallineModel2;
    }

    /**
     * 根据套餐编号查询套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealInfoById(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public MealModel getMealInfoByCode(MealDefineDto dto) throws AppException {
        MealModel mealModel = new MealModel();
        mealModel.setCode(dto.getCode());
        return iMealManageDao.getMealInfoByCode(mealModel);
    }

    /**
     * 根据套餐ID修改套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#updateMealInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public boolean updateMealInfo(MealDefineDto dto) throws AppException {
        if (iMealManageDao.updateMealInfo(dto)) {
            return true;
        }
        return false;
    }

    /**
     * 修改套餐的详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#updateMealLineInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public boolean updateMealLineInfo(MealDefineDto dto) throws AppException {
        if (iMealLineManageDao.updateMealLineInfo(dto)) {
            //根据套餐ID获取套餐的详细信息
            List<MeallineModel> meallineList = getMealLIneInfoByPackageId(dto.getPackageDefId());

            Double sumAmount = 0.00;
            for (Iterator iterator = meallineList.iterator(); iterator.hasNext();) {
                MeallineModel meallineModel = (MeallineModel) iterator.next();

                //根据ticketId查询优惠券和提货券的详细信息
                Long ticketId = meallineModel.getTicketDefId();
                List<TicketLine> ticketlineList = iTicketLineDefineDao.getTicketLineInfoList(Long
                    .toString(ticketId));

                if (!ticketlineList.isEmpty()) {
                    Double prepayAmount = 0.00;
                    for (Iterator iterator2 = ticketlineList.iterator(); iterator2.hasNext();) {
                        TicketLine ticketLine = (TicketLine) iterator2.next();
                        if (ticketId.equals(ticketLine.getTicketDefId())) {
                            prepayAmount = prepayAmount + ticketLine.getPrepayAmount();
                        }
                    }
                    double amount = prepayAmount * meallineModel.getNumberOfUse();
                    BigDecimal bigAmount = new BigDecimal(amount);

                    amount = bigAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sumAmount = sumAmount + amount;
                }
            }

            MealModel mealModel = iMealManageDao.findById(dto.getPackageDefId());
            mealModel.setPrice(sumAmount);
            iMealManageDao.update(mealModel);

            return true;
        }
        return false;
    }

    /**
     * 根据套餐ID查询套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealInfoById(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public MealModel getMealInfoById(MealDefineDto dto) throws AppException {
        MealModel mealModel = new MealModel();
        mealModel.setId(dto.getId());
        return iMealManageDao.getMealInfoById(mealModel);
    }

    /**
     * 根据套餐ID查询套餐详细信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMeallineList(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public List<MealLineResultDto> getMeallineList(MealDefineDto dto) throws AppException {
        List<Object[]> list = iMealLineManageDao.getMeallineList(dto);
        List<MealLineResultDto> mealLineResultList = new ArrayList<MealLineResultDto>();

        if (mealLineResultList != null) {
            for (int i = 0; i < list.size(); i++) {
                MealLineResultDto mealLineResultDto = new MealLineResultDto();
                Object[] obj = (Object[]) list.get(i);
                BigInteger id = (BigInteger) obj[0];
                BigInteger packageId = (BigInteger) obj[1];
                BigInteger ticketId = (BigInteger) obj[6];

                mealLineResultDto.setId(id.longValue());
                mealLineResultDto.setPackageDefId(packageId.longValue());
                mealLineResultDto.setShortName((String) obj[2]);
                mealLineResultDto.setDescription((String) obj[3]);
                mealLineResultDto.setNumberOfUse((Integer) obj[4]);
                mealLineResultDto.setHasVerificationCode((String) obj[5]);
                mealLineResultDto.setTicketDefId(ticketId.longValue());
                mealLineResultDto.setTicketType((String) obj[7]);
                mealLineResultList.add(mealLineResultDto);
            }
        }

        if (!mealLineResultList.isEmpty()) {
            return mealLineResultList;
        }
        return new ArrayList<MealLineResultDto>();
    }

    /**
     * 删除套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removeMealById(java.lang.Long)
     */
    @Override
    public void removeMealById(Long id) throws AppException {
        iMealManageDao.remove(id);//删除套餐信息
        removeMealLineByPackageDefId(id);//删除套餐详细信息
        removeMemberGiftByPackageId(id);//删除套餐入会礼信息
        //删除套餐价目表信息
        //removeMealPriceByPackageId(id)
    }

    /***
     * 根据套餐详细信息的ID查询套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealLIneInfo(java.lang.Long)
     */
    @Override
    public MeallineModel getMealLIneInfo(Long id) throws AppException {
        return iMealLineManageDao.getMealLIneInfo(id);

    }

    /**
     * 根据套餐的ID查询套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealLIneInfoByPackageId(java.lang.Long)
     */
    @Override
    public List<MeallineModel> getMealLIneInfoByPackageId(Long packageDefId) throws AppException {
        return iMealLineManageDao.getMealLIneInfoByPackageId(packageDefId);

    }

    /**
     * 根据套餐ID删除套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removeMealLineByPackageDefId(java.lang.Long)
     */
    @Override
    public void removeMealLineByPackageDefId(Long packageDefId) throws AppException {

        List<MeallineModel> meallineList = getMealLIneInfoByPackageId(packageDefId);
        if (!meallineList.isEmpty()) {
            iMealLineManageDao.removeMealLineByPackageDefId(meallineList);
        }
    }

    /**
     * 根据套餐详细信息的ID删除套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removeMealLineById(java.lang.Long)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public void removeMealLineById(Long id) throws AppException {

        MeallineModel meallineModel = iMealLineManageDao.findById(id);
        Long packageDefId = meallineModel.getPackageDefId();

        iMealLineManageDao.remove(id);

        //根据套餐ID获取套餐的详细信息
        List<MeallineModel> meallineList = getMealLIneInfoByPackageId(packageDefId);

        Double sumAmount = 0.00;
        for (Iterator iterator = meallineList.iterator(); iterator.hasNext();) {
            MeallineModel mealline = (MeallineModel) iterator.next();

            //根据ticketId查询优惠券和提货券的详细信息
            Long ticketId = mealline.getTicketDefId();
            List<TicketLine> ticketlineList = iTicketLineDefineDao.getTicketLineInfoList(Long
                .toString(ticketId));

            if (!ticketlineList.isEmpty()) {
                Double prepayAmount = 0.00;
                for (Iterator iterator2 = ticketlineList.iterator(); iterator2.hasNext();) {
                    TicketLine ticketLine = (TicketLine) iterator2.next();
                    if (ticketId.equals(ticketLine.getTicketDefId())) {
                        prepayAmount = prepayAmount + ticketLine.getPrepayAmount();
                    }
                }
                double amount = prepayAmount * mealline.getNumberOfUse();
                BigDecimal bigAmount = new BigDecimal(amount);

                amount = bigAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                sumAmount = sumAmount + amount;
            }
        }

        MealModel mealModel = iMealManageDao.findById(packageDefId);
        mealModel.setPrice(sumAmount);
        iMealManageDao.update(mealModel);
    }

    /**
     * 根据N个条件查询套餐信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMealModelListByParams(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public List<MealModel> getMealModelListByParams(MealDefineDto mealDefineDto)
                                                                                throws AppException {
        return iMealManageDao.getMealModelListByParams(mealDefineDto);
    }

    /**
     * 入会礼新增
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#saveMermberGiftInfo(com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto)
     */
    @Override
    public boolean saveMermberGiftInfo(MermberGiftDto dto) throws AppException {

        //获取入会礼信息
        String giftStr = dto.getGiftStr();
        if (!"".equals(giftStr) && giftStr != null) {
            String[] gift = giftStr.split("###");
            if (gift != null) {
                for (int i = 0; i < gift.length; i++) {
                    String[] str = gift[i].split("@#");
                    String giftName = str[0];
                    String giftType = str[1];
                    if (giftName.indexOf("_") > 0) {
                        giftName = giftName.substring(giftName.indexOf("_") + 1);
                    }
                    MermberGiftDto mermberGiftDto = new MermberGiftDto();
                    mermberGiftDto.setPackageId(dto.getPackageId());
                    mermberGiftDto.setGiftType(giftType);
                    mermberGiftDto.setGiftId(Long.parseLong(giftName));
                    if (iMemberGiftDao.saveMermberGiftInfo(mermberGiftDto) && i == gift.length - 1) {
                        return true;
                    }
                }
            }
        }
        return false;

    }

    /**
     * 入会礼删除
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removeMermberGift(com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto)
     */
    @Override
    public boolean removeMermberGift(MermberGiftDto dto) throws AppException {
        iMemberGiftDao.remove(dto.getId());
        return true;
    }

    /**
     * 套餐复制
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#saveCopyMealInfo(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> saveCopyMealInfo(MealDefineDto dto) throws AppException {
        //根据套餐ID查询套餐信息
        MealModel mealModel = getMealInfoById(dto);
        MealModel mealModel2 = new MealModel();
        mealModel2.setCode(dto.getCode());
        mealModel2.setName(dto.getName());
        mealModel2.setDescription(mealModel.getDescription());
        mealModel2.setStatus(MealConstants.MEAL_STATUS_DRAFT);
        mealModel2.setPrice(mealModel.getPrice());
        mealModel2.setValidPeriod(mealModel.getValidPeriod());
        mealModel2.setUnitOfPeriod(mealModel.getUnitOfPeriod());
        mealModel2.setIsPhysicalCard(mealModel.getIsPhysicalCard());
        mealModel2.setIsPrepaymentCard(mealModel.getIsPrepaymentCard());
        mealModel2.setNumberSequence(mealModel.getNumberSequence());
        mealModel2.setCardDefId(mealModel.getCardDefId());
        mealModel2.setMemberBenfit(mealModel.getMemberBenfit());

        //新增套餐信息
        iMealManageDao.save(mealModel2);

        MeallineModel meallineModel2 = null;
        //根据套餐ID查询套餐详细信息
        List<MeallineModel> meallineList = getMealLIneInfoByPackageId(dto.getId());
        if (!meallineList.isEmpty()) {
            for (Iterator iterator = meallineList.iterator(); iterator.hasNext();) {
                MeallineModel meallineModel = (MeallineModel) iterator.next();
                meallineModel2 = new MeallineModel();
                meallineModel2.setPackageDefId(mealModel2.getId());
                meallineModel2.setShortName(meallineModel.getShortName());
                meallineModel2.setDescription(meallineModel.getDescription());
                meallineModel2.setNumberOfUse(meallineModel.getNumberOfUse());
                meallineModel2.setHasVerificationCode(meallineModel.getHasVerificationCode());
                meallineModel2.setTicketDefId(meallineModel.getTicketDefId());

                //套餐详细信息新增
                iMealLineManageDao.save(meallineModel2);
            }
        }

        //根据套餐ID查询入会礼信息
        List<MemberGiftInfo> memberGiftInfoList = iMemberGiftDao.getPackageGiftByPackageDefId(dto
            .getId());
        if (!memberGiftInfoList.isEmpty()) {
            MemberGiftInfo memberGiftInfo2 = null;
            for (Iterator iterator = memberGiftInfoList.iterator(); iterator.hasNext();) {
                MemberGiftInfo memberGiftInfo = (MemberGiftInfo) iterator.next();
                memberGiftInfo2 = new MemberGiftInfo();
                memberGiftInfo2.setPackageId(mealModel2.getId());
                memberGiftInfo2.setGiftType(memberGiftInfo.getGiftType());
                memberGiftInfo2.setGiftId(memberGiftInfo.getGiftId());

                //入会礼信息新增
                iMemberGiftDao.save(memberGiftInfo2);

            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", mealModel2.getId());
        map.put("code", true);
        return map;
    }

    /**
     * 根据套餐ID删除入会礼信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removeMemberGiftByPackageId(java.lang.Long)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public void removeMemberGiftByPackageId(Long packageDefId) throws AppException {
        List<MemberGiftInfo> memberGiftList = iMemberGiftDao.getGiftByPackageDefId(packageDefId);
        for (Iterator iterator = memberGiftList.iterator(); iterator.hasNext();) {
            MemberGiftInfo memberGiftInfo = (MemberGiftInfo) iterator.next();
            iMemberGiftDao.remove(memberGiftInfo);
        }
    }

    /**
     * 根据优惠券和提货券的ID查询套餐详细信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getMeallineListByTicketDefId(java.lang.Long)
     */
    @Override
    public List<MeallineModel> getMeallineListByTicketDefId(Long ticketDefId) throws AppException {
        return iMealLineManageDao.getMeallineListByTicketDefId(ticketDefId);
    }

    /**
     * 套餐启用或者停用
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#packageEnabled(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public boolean packageEnabled(MealDefineDto dto) throws AppException {
        return iMealManageDao.packageEnabled(dto);
    }

    /**
     * 查询套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getPackageEligibilityList(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public List<PackageEligibilityModel> getPackageEligibilityList(PackageEligibilityDto packageEligibilityDto)
                                                                                                               throws AppException {
        return iPackageEligibilityDao.getPackageEligibilityList(packageEligibilityDto);
        	
    }

    /**
     * 新增套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#insertPackageEligibilityInfo(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public void insertPackageEligibilityInfo(PackageEligibilityDto packageEligibilityDto)
                                                                                                            throws AppException {
        PackageEligibilityModel packageEligibilityModel = null;
        String dataString = packageEligibilityDto.getDataString();
        String[] datas = dataString.split("###");
        for (int i = 0; i < datas.length; i++) {
            String[] dataString2 = datas[i].split("@#");
            String dealerProperty = dataString2[0];
            String propertyValue = dataString2[1]; 
            Long packageDefId = Long.parseLong(dataString2[2]);
            String codeType = dataString2[3]; 
            
            packageEligibilityModel = new PackageEligibilityModel();
            packageEligibilityModel.setPackageDefId(packageDefId);
            packageEligibilityModel.setDealerProperty(dealerProperty);
            packageEligibilityModel.setPropertyValue(propertyValue);
            packageEligibilityModel.setCodeType(codeType);
            
            iPackageEligibilityDao.insertPackageEligibilityInfo(packageEligibilityModel);
        }
    }

    /**
     * 删除套餐的适用范围信息
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#removePackageEligibilityInfo(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public boolean removePackageEligibilityInfo(PackageEligibilityDto packageEligibilityDto)
                                                                                            throws AppException {
        if (iPackageEligibilityDao.removePackageEligibilityInfo(packageEligibilityDto)) {
            return true;
        }
        return false;
        	
    }

    /**
     * 根据套餐ID,Dealer_property,Property_value查询套餐的适用范围
     * @see com.cap.bts.crmdp.dealer.meal.service.IMealManageService#getPackageEligibilityInfoByPackageDefId(com.cap.bts.crmdp.dealer.meal.dto.PackageEligibilityDto)
     */
    @Override
    public PackageEligibilityModel getPackageEligibilityInfoByPackageDefId(PackageEligibilityDto packageEligibilityDto)
                                                                                                       throws AppException {
        return iPackageEligibilityDao.getPackageEligibilityInfoByPackageDefId(packageEligibilityDto);
        	
    }

	@Override
	public void savePackageSelected(PackageDealerSelected obj) throws AppException {
		try {
			packageDealerSelectedDao.save(obj);
		} catch (Exception e) {
			LOGGER.error("savePackageSelected ERROR" + e);
		}
	}

	@Override
	public List<PackageDealerSelected> getPackageSelected(String packageDefId,String dealerId) throws AppException {
		return packageDealerSelectedDao.getPackageSelected(packageDefId,dealerId);
	}

	@Override
	public void removePackageSelected(String dealerId) throws AppException {
		try {
			packageDealerSelectedDao.removePackageSelected(dealerId);
		} catch (Exception e) {
			LOGGER.error("removePackageSelected ERROR" + e);
		}
	}

}
